/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package quanlysieuthi;

import DTO.*;
import DAO.*;
import java.util.*;
import Model.CModel;
import java.awt.Point;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class FrmKhachHang extends javax.swing.JFrame {

    /**
     * Creates new form FrmKhachHang
     */
    public FrmKhachHang() {
        initComponents();
        this.dtcNgay_sinh.setDateFormatString(QuanLySieuThi.formatDate);
        isSetEnabled(true);
        this.add = false;
        this.edit = false;
        setTextLabel();
        loadKhachHang();
    }
    
    private int loai = 1;//1 khach hang. 2 nha cung cap
    
    private void defaultValue(){
        String mysql = "";
        mysql = String.format("select * from khach_hang where id in(select Max(id) from khach_hang where loai=%s)",loai);
        khDAO = new CKhachHangDAO();
        config = SubConfig.getInstance();
        config.init();
        List<CKhachHang> lCode = khDAO.getAll(mysql, config);
        String scode = "0000000001";
        if(lCode.size() > 0){
            CKhachHang temp = lCode.get(0);
            scode = temp.getMaThe();
            scode = CKiemTra.MaTuTang(scode);
        }
        this.txtma_the.setText(scode);
        this.txtma_the.setEditable(false);
        config.close();
    }
    
    private void setTextLabel(){
        String strTieude = "QUẢN LÝ THÔNG TIN KHÁCH HÀNG";
        
        if(this.loai == 2){
            strTieude = "QUẢN LÝ THÔNG TIN NHÀ CUNG CẤP";
        }
        lbltieu_de.setText(strTieude);
    }
    
    public FrmKhachHang(int loai) {
        initComponents();
        this.btnsave.enable(false);
        this.add = false;
        this.edit = false;
        this.loai = loai;
        setTextLabel();
        loadKhachHang();
        txtma_the.setEditable(false);
    }
    Boolean add = false;
    Boolean edit = false;
    List<CKhachHang>lKh = new ArrayList<CKhachHang>();
    CKhachHangDAO khDAO = new CKhachHangDAO();
    SubConfig config = null;
    int currentIndex = 0;
    int numberRow = 0;
    int idselect = -1;
    long id = -1;
    CKhachHang currentKH = null;
    private void loadKhachHang(){
        config = SubConfig.getInstance();
        config.init();
        khDAO.sWhere = " where loai = " + loai;
        lKh = khDAO.getAll(config);
        Vector<Vector> data = new Vector<Vector>();
        Boolean flag = false;
        for(CKhachHang item : lKh){
            Vector<String> row = new Vector<String>();
            row.addElement(item.getHoTen());
            row.addElement(item.getDiaChi());
            row.addElement(item.getDienThoai());
            row.addElement(item.getMaThe());
            row.addElement(""+item.getDiemTichLuy());
            data.add(row);
            flag = true;
        }
        Vector<String> columnNames = new Vector<String>();
        columnNames.addElement("Tên");
        columnNames.addElement("Đia chi");
        columnNames.addElement("Điện thọai");
        columnNames.addElement("Mã thẻ");
        columnNames.addElement("Điểm");
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        //tbldanhsach.
        this.tbldanhsach.removeAll();
        this.tbldanhsach.setModel(model);
        this.tbldanhsach.setFillsViewportHeight(true);
        this.tbldanhsach.setEnabled(false);
        currentIndex = 0;
        numberRow = lKh.size();
        view(currentIndex);
        this.setEditable(false);
    }
    
    private void clearText(){
        this.txtCMND.setText("");
        this.txtSoDiem.setText("0");
        this.txtdia_chi.setText("");
        this.txtdien_thoai.setText("");
        this.dtcNgay_sinh.setDate(new Date());
        cbxnhom_khach_hang.setSelectedIndex(-1);
        txtten_khach_hang.setText("");
        
    }
    private void setEditable(Boolean b){
        this.txtCMND.setEditable(b);
        this.txtCMND.setEditable(b);
        this.txtSoDiem.setEditable(b);
        this.txtdia_chi.setEditable(b);
        this.txtdien_thoai.setEditable(b);
        this.dtcNgay_sinh.setDate(new Date());
        cbxnhom_khach_hang.setEnabled(b);
        txtten_khach_hang.setEditable(b);        
    }
    
    private Boolean insertKhachHang(){
        CModel _model = new CModel(QuanLySieuThi.user_name);
        int diem_tich_luy = Integer.parseInt(txtSoDiem.getText());
        int nhom_kh = this.cbxnhom_khach_hang.getSelectedIndex();
        String ma_the = txtma_the.getText();
        _model.insertKhachHang(this.loai, this.txtten_khach_hang.getText(), this.txtdia_chi.getText(), this.txtdien_thoai.getText()
                , "", diem_tich_luy, nhom_kh, ma_the);
        _model.saveHisTory();
        _model.Close();
        loadKhachHang();
        return true;
    }
    
    private void view(int index){
        id = -1;
        currentKH = null;
        if(index >=0 && index < this.numberRow){
            CKhachHang item = lKh.get(index);
            currentKH = item;
           txtma_the.setText(item.getMaThe());
           txtdia_chi.setText(item.getDiaChi());
           txtten_khach_hang.setText(item.getHoTen());
           id = item.getId();
           this.tbldanhsach.setRowSelectionInterval(index, index);
           txtRows.setText(String.format("%s/%s", index + 1,numberRow));
        }
    }
    
    private Boolean updateKhachHang(){
        //idselect
        CModel _model = new CModel(QuanLySieuThi.user_name);
        int diem_tich_luy = Integer.parseInt(txtSoDiem.getText());
        int nhom_kh = this.cbxnhom_khach_hang.getSelectedIndex();
        String ma_the = txtma_the.getText();
      
        _model.updateKhachHang(this.loai, this.txtten_khach_hang.getText(), this.txtdia_chi.getText(), this.txtdien_thoai.getText()
                , "", diem_tich_luy, nhom_kh, ma_the,id);
        _model.saveHisTory();
        _model.Close();
        loadKhachHang();
        return true;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnadd = new javax.swing.JButton();
        btnedit = new javax.swing.JButton();
        btnsave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnclose = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtma_the = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtten_khach_hang = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtdien_thoai = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtdia_chi = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtemail = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtCMND = new javax.swing.JTextField();
        cbxnhom_khach_hang = new javax.swing.JComboBox();
        jLabel14 = new javax.swing.JLabel();
        txtSoDiem = new javax.swing.JTextField();
        dtcNgay_sinh = new com.toedter.calendar.JDateChooser();
        lbltieu_de = new javax.swing.JLabel();
        btndelete = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbldanhsach = new javax.swing.JTable();
        btnre_end1 = new javax.swing.JButton();
        btnre_one1 = new javax.swing.JButton();
        txtRows = new javax.swing.JTextField();
        btnnext_one1 = new javax.swing.JButton();
        btnnext_end1 = new javax.swing.JButton();

        btnadd.setText("Thêm mới");
        btnadd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaddActionPerformed(evt);
            }
        });

        btnedit.setText("Sửa");
        btnedit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditActionPerformed(evt);
            }
        });

        btnsave.setText("Lưu");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });

        btnCancel.setText("Hủy");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnclose.setText("Thoát");
        btnclose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncloseActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin khách hàng"));
        jPanel1.setPreferredSize(new java.awt.Dimension(820, 185));

        jLabel1.setText("Mã thẻ:");

        txtma_the.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtma_theActionPerformed(evt);
            }
        });

        jLabel2.setText("Tên :");

        jLabel3.setText("Điện thoại:");

        jLabel4.setText("Địa chỉ:");

        jLabel5.setText("Email:");

        jLabel6.setText("Ngày sinh:");

        jLabel7.setText("Nhóm khách hàng:");

        jLabel8.setText("Số CNMD:");

        cbxnhom_khach_hang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Khách hàng lẻ", "Khách hàng thân thiết", "Khách hàng vip" }));

        jLabel14.setText("Số điểm hiện có:");

        txtSoDiem.setText("0");

        dtcNgay_sinh.setDateFormatString("dd-MM-yyyy");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(34, 34, 34)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtma_the, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtdien_thoai, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(dtcNgay_sinh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtdia_chi, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtten_khach_hang, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(12, 12, 12)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtSoDiem, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbxnhom_khach_hang, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel14))
                .addGap(16, 16, 16))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtma_the, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtdien_thoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(txtten_khach_hang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel4)
                                    .addComponent(txtdia_chi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel8))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(31, 31, 31)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel7)
                                        .addComponent(cbxnhom_khach_hang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel14)
                            .addComponent(txtSoDiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dtcNgay_sinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
        );

        lbltieu_de.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lbltieu_de.setForeground(new java.awt.Color(102, 102, 255));
        lbltieu_de.setText("QUẢN LÝ THÔNG TIN KHÁCH HÀNG");

        btndelete.setText("Xóa");
        btndelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndeleteActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách khách hàng thân thiết:"));

        tbldanhsach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbldanhsach.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbldanhsachMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbldanhsach);

        btnre_end1.setText("<<");
        btnre_end1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnre_end1ActionPerformed(evt);
            }
        });

        btnre_one1.setText("<");
        btnre_one1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnre_one1ActionPerformed(evt);
            }
        });

        txtRows.setText("1/10");

        btnnext_one1.setText(">");
        btnnext_one1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnext_one1ActionPerformed(evt);
            }
        });

        btnnext_end1.setText(">>");
        btnnext_end1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnext_end1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 823, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnre_end1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnre_one1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtRows, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnnext_one1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnnext_end1)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnre_one1)
                    .addComponent(btnnext_one1)
                    .addComponent(btnre_end1)
                    .addComponent(txtRows, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnnext_end1))
                .addGap(239, 239, 239))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(12, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lbltieu_de)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 916, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnadd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnedit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnsave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btndelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnclose)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(39, 39, 39))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(lbltieu_de)
                .addGap(20, 20, 20)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnadd)
                    .addComponent(btnedit)
                    .addComponent(btnsave)
                    .addComponent(btnCancel)
                    .addComponent(btnclose)
                    .addComponent(btndelete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void txtma_theActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtma_theActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtma_theActionPerformed

    private void btncloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncloseActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btncloseActionPerformed

    private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaddActionPerformed
        // TODO add your handling code here:
        this.add = true;
        this.edit = false;        
        this.isSetEnabled(false);
        this.clearText();
        this.setEditable(true);
        defaultValue();
    }//GEN-LAST:event_btnaddActionPerformed

    private void btneditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneditActionPerformed
        // TODO add your handling code here:
        this.edit = true;
        this.add = false;
        this.isSetEnabled(false);
        this.setEditable(true);
    }//GEN-LAST:event_btneditActionPerformed

    private void isSetEnabled(Boolean b){
        Boolean flag = b == true ? false : true;
        this.btnsave.setEnabled(flag);
        this.btnCancel.setEnabled(flag);
        this.btnedit.setEnabled(b);        
        this.btnadd.setEnabled(b);
        this.btndelete.setEnabled(b);
    }
    
    private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsaveActionPerformed
        // TODO add your handling code here:
        Boolean flag = false;
        if(this.add){
            flag = this.insertKhachHang();
        }
        else if(this.edit){
            flag = updateKhachHang();
        }
        if(flag){
            this.add = false;
            this.edit = false;
            this.isSetEnabled(true);
        }
    }//GEN-LAST:event_btnsaveActionPerformed

    private void tbldanhsachMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbldanhsachMouseClicked
        // TODO add your handling code here:
        JTable table =(JTable)evt.getSource();
        Point p = evt.getPoint();
        int rowIndex = table.rowAtPoint(p);       
        if(rowIndex >=0 && rowIndex < this.numberRow){           
            this.currentIndex = rowIndex;
            view(rowIndex);
        }
    }//GEN-LAST:event_tbldanhsachMouseClicked

    private void btnre_one1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnre_one1ActionPerformed
        // TODO add your handling code here:
        if(this.currentIndex > 0){
            this.currentIndex --;
            this.view(this.currentIndex);
        }        
    }//GEN-LAST:event_btnre_one1ActionPerformed

    private void btnre_end1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnre_end1ActionPerformed
        // TODO add your handling code here:
        
        this.currentIndex = 0;
        this.view(this.currentIndex);        
    }//GEN-LAST:event_btnre_end1ActionPerformed

    private void btnnext_one1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnext_one1ActionPerformed
        // TODO add your handling code here:
         if(this.currentIndex < this.numberRow){
            this.currentIndex ++;
            this.view(this.currentIndex);
        }  
    }//GEN-LAST:event_btnnext_one1ActionPerformed

    private void btnnext_end1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnext_end1ActionPerformed
        // TODO add your handling code here:
        this.currentIndex = this.numberRow - 1;
        this.view(this.currentIndex);      
    }//GEN-LAST:event_btnnext_end1ActionPerformed

    private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btndeleteActionPerformed
        // TODO add your handling code here:
        if(currentKH == null)
            return;
        int reply = JOptionPane.showConfirmDialog(this, "bạn có muốn xóa dòng này ko? " + currentKH.getHoTen());
        if(reply == JOptionPane.YES_OPTION){
            CModel _model = new CModel(QuanLySieuThi.user_name);
            _model.deleteKhachHang(currentKH);
            _model.saveHisTory();
            _model.Close();
            this.loadKhachHang();
        }
    }//GEN-LAST:event_btndeleteActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        this.loadKhachHang();
        this.isSetEnabled(true);
    }//GEN-LAST:event_btnCancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmKhachHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmKhachHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmKhachHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmKhachHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmKhachHang().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnadd;
    private javax.swing.JButton btnclose;
    private javax.swing.JButton btndelete;
    private javax.swing.JButton btnedit;
    private javax.swing.JButton btnnext_end1;
    private javax.swing.JButton btnnext_one1;
    private javax.swing.JButton btnre_end1;
    private javax.swing.JButton btnre_one1;
    private javax.swing.JButton btnsave;
    private javax.swing.JComboBox cbxnhom_khach_hang;
    private com.toedter.calendar.JDateChooser dtcNgay_sinh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbltieu_de;
    private javax.swing.JTable tbldanhsach;
    private javax.swing.JTextField txtCMND;
    private javax.swing.JTextField txtRows;
    private javax.swing.JTextField txtSoDiem;
    private javax.swing.JTextField txtdia_chi;
    private javax.swing.JTextField txtdien_thoai;
    private javax.swing.JTextField txtemail;
    private javax.swing.JTextField txtma_the;
    private javax.swing.JTextField txtten_khach_hang;
    // End of variables declaration//GEN-END:variables
}
